<template>
  <a-modal
    :title="formTile + '实时人群包'"
    :visible="true"
    width="50%"
    :footer="null"
    @cancel="reset"
  >
    <div class="main">
      <a-card>
        <a-row
          type="flex"
          justify="start"
          align="top"
        >
          <a-col :span="24">
            <a-form
              :form="form"
              @submit="handleSubmit"
              :labelCol="formItemLayout.labelCol"
              :wrapperCol="formItemLayout.wrapperCol"
            >
              <a-form-item
                label="人群包名称"
                :required="true"
              >
                <a-input
                  allow-clear
                  placeholder="请输入人群包名称"
                  :maxLength="40"
                  v-decorator="[
                  `peoplesName`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入人群包名称'
                      }
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="应用类型"
                :required="true"
              >
                <a-select
                  allow-clear
                  show-search
                  placeholder="请选择应用类型"
                  optionFilterProp="children"
                  @change="changeType"
                  v-decorator="[
                  'appType',
                  {
                    rules: [{ required: true, message: '请选择应用类型!' }]
                  }
                ]"
                >
                  <a-select-option
                    :value="item.id"
                    v-for="(item,index) in useTypeList"
                    :key="index"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item
                label="人群包类型"
                :required="true"
                v-if="showPeople"
              >
                <a-select
                  allow-clear
                  show-search
                  placeholder="请选择人群包类型"
                  optionFilterProp="children"
                  @change="changePeople"
                  v-decorator="[
                  'peoplesType',
                  {
                    rules: [{ required: true, message: '请选择人群包类型!' }]
                  }
                ]"
                >
                  <a-select-option
                    :value="item.id"
                    v-for="(item,index) in realTimePeoplesTypes"
                    :key="index"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item
                label="归属渠道"
                :required="true"
              >
                <a-select
                  allow-clear
                  show-search
                  placeholder="请选择归属渠道"
                  optionFilterProp="children"
                  v-decorator="[
                  'channel',
                  {
                    rules: [{ required: true, message: '请选择归属渠道!' }]
                  }
                ]"
                >
                  <a-select-option
                    :value="item.value"
                    v-for="(item,index) in chanelList"
                    :key="index"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>

              </a-form-item>
              <a-form-item label="人群包配置">
              </a-form-item>
              <div style="background: #fafafa;padding: 30px 0 10px 0;">
                <a-form-item
                  label="未消费时间"
                  :required="true"
                  v-if="people == 50"
                >
                  <a-input-number
                    allow-clear
                    placeholder="请输入首消后间隔的未消费时间"
                    style="width:150px"
                    :min="0"
                    :precision="0"
                    :max="99999"
                    @change="changeNoUse"
                    oninput="if(value.length>5)value=value.slice(0,5)"
                    v-decorator="[
                  `consumptionTime`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入首消后间隔的未消费时间'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="品牌"
                  v-if="people != 60 && people != 40"
                  :required="true"
                >
                  <a-select
                    allow-clear
                    show-search
                    placeholder="请选择品牌"
                    optionFilterProp="children"
                    @change="changebrand"
                    v-decorator="[
                  'bandBrand',
                  {
                    rules: [{ required: true, message: '请选择品牌!' }]
                  }
                ]"
                  >
                    <a-select-option
                      :value="item"
                      v-for="(item,index) in brandList"
                      :key="index"
                    >
                      {{ item }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="门店"
                  :required="true"
                  v-if="type == 1 || type == 5"
                >
                  <div style="display: flex;justify-content: space-between;margin-top: 5px;">
                    <a-select
                      allow-clear
                      style="width:180px"
                      show-search
                      placeholder="请选择门店"
                      optionFilterProp="children"
                      @change="changeStore"
                      v-decorator="[
                  'isBandStore',
                  {
                    rules: [{ required: true, message: '请选择门店!' }]
                  }
                ]"
                    >
                      <a-select-option :value="0">
                        全部门店
                      </a-select-option>
                      <a-select-option :value="1">
                        指定门店
                      </a-select-option>

                    </a-select>
                    <div
                      style="margin-top: -5px;margin-left:5px"
                      id="export"
                    >
                      <a-upload
                        name="file"
                        v-if="store == 1"
                        :file-list="file"
                        :beforeUpload="beforeUpload"
                        :remove="removeFile"
                        action="https://jic.jnby.com/activity/market-act/common/upload/multipart"
                      >
                        <a-button>
                          <a-icon type="upload" /> 导入门店
                        </a-button>
                      </a-upload>
                    </div>
                    <a-popover
                      title="当前门店"
                      v-if="store == 1"
                    >
                      <template slot="content">
                        <div style="width:550px;">
                          <a-row>
                            <a-col
                              :span="12"
                              v-for="(item,index) in realImportStoreList"
                              :key="index"
                            >
                              {{item.storeName}}
                            </a-col>
                            <a-col v-if="!realImportStoreList">
                              请先导入门店
                            </a-col>
                          </a-row>
                        </div>
                      </template>
                      <a-button type="link">
                        查看当前门店
                      </a-button>

                    </a-popover>
                  </div>

                </a-form-item>
                <a-form-item
                  label="类型"
                  :required="true"
                  v-if="type == 1"
                >
                  <a-select
                    allow-clear
                    show-search
                    placeholder="请选择类型"
                    optionFilterProp="children"
                    @change="changeMouth"
                    v-decorator="[
                  'type',
                  {
                    rules: [{ required: true, message: '请选择类型!' }]
                  }
                ]"
                  >
                    <a-select-option
                      :value="item.value"
                      v-for="(item,index) in typeList"
                      :key="index"
                    >
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="会员等级"
                  :required="true"
                  v-if="type == 1 || type == 4"
                >
                  <a-select
                    allow-clear
                    show-search
                    mode="multiple"
                    @change="changeMember"
                    placeholder="请选择会员等级"
                    optionFilterProp="children"
                    v-decorator="[
                  'memberLevel',
                  {
                    rules: [{ required: true, message: '请选择会员等级!' }]
                  }
                ]"
                  >
                    <a-select-option
                      :value="item.value"
                      v-for="(item,index) in cardList"
                      :key="index"
                    >
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="开卡天数"
                  :required="true"
                  v-if="type == 1"
                >
                  <a-input-number
                    allow-clear
                    style="width:150px"
                    :min="0"
                    :precision="0"
                    :max="99999"
                    @change="changeDay"
                    placeholder="请输入开卡天数"
                    v-decorator="[
                  `cardDay`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入开卡天数'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="券ID"
                  :required="true"
                  v-if="people == 20 || people == 40 || people == 60 || people == 80"
                >
                  <a-input
                    allow-clear
                    placeholder="如有多个用”,“隔开"
                    @change="changeCardId"
                    v-decorator="[
                  `bandTicket`,
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入券ID'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="券发放天数"
                  :required="true"
                  v-if="people == 40 || people ==60"
                >
                  <a-input-number
                    allow-clear
                    placeholder="请输入券发放天数"
                    style="width:150px"
                    :min="0"
                    :precision="0"
                    :max="99999"
                    @change="changeTday"
                    v-decorator="[
                  `ticketDay`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入券发放天数'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="关注公众号时间"
                  :required="true"
                  v-if="type == 6"
                  help="单位：小时（取整，例如3小时45分钟，取整3小时）"
                >
                  <span>≥</span>
                  <a-input-number
                    allow-clear
                    placeholder="请输入"
                    style="width:100px;margin-left:10px"
                    :min="0"
                    :precision="0"
                    :max="999999"
                    @change="changePublic"
                    v-decorator="[
                  `subTime`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入关注公众号时间'
                      }
                    ]
                  }
                ]"
                  />
                  <span style="margin-left:10px">小时，＜</span>
                  <a-input-number
                    allow-clear
                    placeholder="请输入"
                    style="width:100px;margin-left:10px"
                    :min="0"
                    :precision="0"
                    :max="999999"
                    @change="changePublic1"
                    v-decorator="[
                  `subTime1`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入关注公众号时间'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="页面路径"
                  v-if="type == 6 || type == 7"
                  :required="true"
                >
                  <a-input
                    allow-clear
                    placeholder="请输入页面路径"
                    @change="changePageUrl"
                    v-decorator="[
                  `pageUrl`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入页面路径'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="进入页面时间"
                  :required="true"
                  v-if="type == 7"
                  help="单位：小时（取整，例如3小时45分钟，取整3小时）"
                >
                  <span>≥</span>
                  <a-input-number
                    allow-clear
                    placeholder="请输入进入页面时间"
                    style="width:100px;margin-left:10px"
                    :min="0"
                    :precision="0"
                    :max="999999"
                    @change="changePageTime"
                    v-decorator="[
                  `buttonPageTime`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入进入页面时间'
                      }
                    ]
                  }
                ]"
                  />
                  <span style="margin-left:10px">小时，＜</span>
                  <a-input-number
                    allow-clear
                    placeholder="请输入进入页面时间"
                    style="width:100px;margin-left:10px"
                    :min="0"
                    :precision="0"
                    :max="999999"
                    @change="changePageTime1"
                    v-decorator="[
                  `buttonPageTime1`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入进入页面时间'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="中心化运营企微配置"
                  v-if="type == 6 || type == 7"
                  :required="true"
                >
                  <a-input
                    allow-clear
                    placeholder="请输入中心化运营企微配置"
                    @change="changeqyConfig"
                    v-decorator="[
                  `qyConfig`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入中心化运营企微配置'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
                <a-form-item
                  label="服务区部"
                  v-if="type == 8"
                  :required="true"
                >
                  <a-select
                    allow-clear
                    show-search
                    @change="changeServe"
                    mode="multiple"
                    placeholder="请选择服务区部"
                    optionFilterProp="children"
                    v-decorator="[
                  'serverDis',
                  {
                    rules: [{ required: true, message: '请选择服务区部!' }]
                  }
                ]"
                  >
                    <a-select-option
                      :value="item.id"
                      v-for="(item,index) in departList"
                      :key="index"
                    >
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="店铺管理模式"
                  v-if="type == 8"
                  :required="true"
                >
                  <a-select
                    allow-clear
                    show-search
                    @change="changeMode"
                    placeholder="请选择店铺管理模式"
                    optionFilterProp="children"
                    v-decorator="[
                  'storeMode',
                  {
                    rules: [{ required: true, message: '请选择店铺管理模式!' }]
                  }
                ]"
                  >
                    <a-select-option
                      :value="item.value"
                      v-for="(item,index) in manageList"
                      :key="index"
                    >
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="消费金额"
                  v-if="type == 8"
                  :required="true"
                >
                  <div
                    v-for="(item, index) in fileds"
                    :key="index"
                  >
                    <a-row :gutter="16">
                      <a-col :span="10">
                        <a-select
                          allow-clear
                          show-search
                          @change="changeNumtype"
                          placeholder="请选择类型"
                          v-decorator="[
                  `realTimeNumberLimits[${index}][type]`,
                  {
                    validateTrigger: ['change', 'blur'],
                  initialValue: item['type']
                  }
                ]"
                        >
                          <a-select-option
                            :value="item.value"
                            v-for="(item,index) in numType"
                            :key="index"
                          >
                            {{ item.name }}
                          </a-select-option>
                        </a-select>

                      </a-col>
                      <a-col :span="14">
                        <a-input
                          allow-clear
                          type="number"
                          @change="changeNum"
                          placeholder="请输入金额"
                          :maxLength="40"
                          v-decorator="[
                  `realTimeNumberLimits[${index}][num]`,
                  {
                    validateTrigger: ['change', 'blur'],
                   initialValue: item['num']
                  }
                ]"
                        />
                      </a-col>
                      <a-icon
                        v-if="index == 0"
                        class="dynamic-delete-button mt-10"
                        type="plus-circle"
                        @click="addField"
                      />
                      <a-icon
                        v-else
                        class="dynamic-delete-button mt-10"
                        type="minus-circle-o"
                        @click="() => remove(index)"
                      />

                    </a-row>
                  </div>
                </a-form-item>
                <a-form-item
                  label="间隔天数"
                  :required="true"
                  v-if="type == 8"
                >
                  <a-input-number
                    allow-clear
                    placeholder="请输入间隔天数"
                    style="width:250px"
                    :min="0"
                    :precision="0"
                    :max="99999"
                    @change="changeJumpDays"
                    v-decorator="[
                  `jumpDays`,
                  {
                    type: 'number',
                    rules: [
                      {
                        required: true,
                        message: '请输入间隔天数'
                      }
                    ]
                  }
                ]"
                  />
                </a-form-item>
              </div>
              <div style="background: #fafafa;padding: 10px 0 10px 0;margin-top:20px;justify-content: center;display: flex;">
                <span
                  v-if="type == 1 && people ==10"
                  style="white-space: pre-line;"
                >
                  <span style="color:#409eff;">{{brand}}</span>品牌下所属门店为<span style="color:#409eff;">{{storeName}}</span>的<br>且开卡时间大于<span style="color:#409eff;">{{cardday}}</span>天的<span style="color:#409eff;">{{member}}</span>会员且<span style="color:#409eff;">{{mouth}}</span>月为当月的会员
                </span>
                <span
                  v-if="type == 1 && people ==20"
                  style="white-space: pre-line;"
                >
                  <span style="color:#409eff;">{{brand}}</span>品牌下所属门店为<span style="color:#409eff;">{{storeName}}</span>的<br>且开卡时间大于<span style="color:#409eff;">{{cardday}}</span>天的<span style="color:#409eff;">{{member}}</span>会员且<span style="color:#409eff;">{{mouth}}</span>月为当月的会员<br>且未核销<span style="color:#409eff;">{{cardId}}</span>券的会员
                </span>
                <span
                  v-if="type == 2 && people ==30"
                  style="white-space: pre-line;"
                >
                  前一日<span style="color:#409eff;">{{brand}}</span>品牌下开卡且未消费的会员
                </span>
                <span
                  v-if="type == 2 && people ==40"
                  style="white-space: pre-line;"
                >
                  拥有<span style="color:#409eff;">{{cardId}}</span>券<br>且券发放时间等于<span style="color:#409eff;">{{tDay}}</span>天<br>且未核销的会员<br><br>注：券只能在当前场景发放
                </span>
                <span
                  v-if="type == 3 && people ==50"
                  style="white-space: pre-line;"
                >
                  <span style="color:#409eff;">{{brand}}</span>品牌下首次消费间隔时间等于<span style="color:#409eff;">{{noUseDay}}</span>天<br>且未再次消费的会员
                </span>
                <span
                  v-if="type == 3 && people ==60"
                  style="white-space: pre-line;"
                >
                  拥有<span style="color:#409eff;">{{cardId}}</span>券<br>且券发放时间等于<span style="color:#409eff;">{{tDay}}</span>天<br>且未核销以上任一券的会员<br><br>注：券只能在当前场景发放
                </span>
                <span
                  v-if="type == 4 && people ==70"
                  style="white-space: pre-line;"
                >
                  <span style="color:#409eff;">{{brand}}</span>品牌下当前月为入会周年月且<br>是<span style="color:#409eff;">{{member}}</span>的会员
                </span>
                <span
                  v-if="type == 4 && people ==80"
                  style="white-space: pre-line;"
                >
                  <span style="color:#409eff;">{{brand}}</span>品牌下当前月为入会周年月且<br>是<span style="color:#409eff;">{{member}}</span>的会员<br>且<span style="color:#409eff;">{{cardId}}</span>券的会员
                </span>
                <span
                  v-if="type == 5"
                  style="white-space: pre-line;"
                >
                  <span style="color:#409eff;">{{brand}}</span>品牌<span style="color:#409eff;">{{storeName}}</span>上个月绑定了专属导购的会员且<br>没有加专属导购企微微信的会员
                </span>
                <span
                  v-if="type == 6"
                  style="white-space: pre-line;"
                >
                  关注<span style="color:#409eff;">{{brand}}</span>品牌公众号的会员<br>且<span style="color:#409eff;">{{brand}}</span>品牌下没有专属导购<br>且关注公众号时间≥<span style="color:#409eff;">{{subTime}}</span>小时，＜<span style="color:#409eff;">{{subTime1}}</span>小时<br>且未添加手机号为<span style="color:#409eff;">{{qyConfig}}</span>的<br>江南布衣员工企微<br>且未进入过<span style="color:#409eff;">{{pageUrl}}</span>页面
                </span>
                <span
                  v-if="type == 7"
                  style="white-space: pre-line;"
                >
                  进入过<span style="color:#409eff;">{{pageUrl}}</span>页面<br>且<span style="color:#409eff;">{{brand}}</span>品牌下没有专属导购<br>且进入页面时间≥<span style="color:#409eff;">{{buttonPageTime}}</span>小时，＜<span style="color:#409eff;">{{buttonPageTime1}}</span>小时<br>且未添加手机号为<span style="color:#409eff;">{{qyConfig}}</span>的<br>江南布衣员工企微
                </span>
                <span
                  v-if="type == 8"
                  style="white-space: pre-line;"
                >
                  品牌为<span style="color:#409eff;">{{brand}}</span>的<span style="color:#409eff;">{{mode}}门店</span>首次消费会员<br>单笔支付实际金额【 <span
                    style="color:#409eff;"
                    v-for="item,index in realTimeNumberLimits"
                    :key="index"
                  >
                    <span
                      v-for="subitem,i in numType"
                      :key="i"
                    >
                      <span v-if="subitem.value == item.type">{{subitem.icon}}{{item.num}} <span v-if="realTimeNumberLimits.length>1 && index!=realTimeNumberLimits.length-1">, </span> </span>
                    </span>
                  </span>】<br>消费时间=<span style="color:#409eff;">{{jumpDays}}</span>天<br>且消费渠道为线下
                </span>
                <span v-if="!type || (!people && type<5)">提示</span>
              </div>
              <a-row style="margin-top:10px">
                <a-col
                  :span="8"
                  :offset="4"
                >
                  <div
                    @click="reset"
                    class="reset"
                  >
                    取消
                  </div>

                </a-col>
                <a-col :span="8">
                  <div
                    class="submit"
                    :loading="loading"
                    @click="handleSubmit"
                  >
                    提交
                  </div>
                </a-col>
              </a-row>
            </a-form>
          </a-col>
        </a-row>
      </a-card>
    </div>

  </a-modal>
</template>

<script>
import { axios } from '@/utils/request'
import { insertPeoples, updatePeoples, useTypeList, brandList, getSetDetail, getRelStore, getCDepartList } from '@api/label/index'
const chanelList = [
  {
    value: 'member',
    name: '会员部'
  },
  {
    value: 'pos',
    name: 'POS+'
  },
  {
    value: 'integral',
    name: '积分商城'
  },
  {
    value: 'damo',
    name: '达摩'
  }
]
const cardList = [
  {
    value: '0',
    name: '银卡'
  },
  {
    value: '1',
    name: '金卡'
  },
  {
    value: '2',
    name: '白金卡'
  },
  {
    value: '3',
    name: '铂金卡'
  }
]
const typeList = [
  {
    value: '0',
    name: '成人生日'
  },
  {
    value: '1',
    name: '孩子生日'
  }
]
const manageList = [
  {
    value: 0,
    name: '正价'
  },
  {
    value: 1,
    name: '奥莱'
  },
  {
    value: 2,
    name: '全部'
  }
]
const numType = [
  {
    value: 'equalTo',
    name: '等于',
    icon: '='
  },
  {
    value: 'greaterThan',
    name: '大于',
    icon: '>'
  },
  {
    value: 'greaterThanEqual',
    name: '大于等于',
    icon: '>='
  },
  {
    value: 'lessThan',
    name: '小于',
    icon: '<'
  },
  {
    value: 'lessThanEqual',
    name: '小于等于',
    icon: '<='
  }
]
export default {
  components: {},

  props: {
    detailId: {
      type: Number,
      default: null
    }
  },

  data() {
    return {
      chanelList,
      cardList,
      typeList,
      manageList,
      numType,
      useTypeList: [],
      brandList: [],
      showPeople: true,
      realTimePeoplesTypes: [],
      urlKey: 'account',
      id: '',
      type: '',
      people: '',
      serve: '',
      add: false,
      store: false,
      loading: false,
      file: '',
      appType: undefined,
      patt: 'api/gateway/basemgt-op/',
      form: this.$form.createForm(this),
      groupId: '',
      detail: {},
      fileds: [],
      exportStore: [],
      departList: [],
      fileList: '',
      file: [],
      realImportStoreList: undefined,
      brand: ' ',
      storeName: ' ',
      mouth: ' ',
      cardday: ' ',
      jumpDays: '',
      member: ' ',
      cardId: ' ',
      realTimeNumberLimits: [],
      buttonPageTime: '',
      buttonPageTime1: '',
      qyConfig: '',
      pageUrl: '',
      tDay: ' ',
      subTime: '',
      subTime1: '',
      noUseDay: ' ',
      mode: '',
      formItemLayout: {
        labelCol: {
          xl: { span: 6 },
          xs: { span: 5 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xl: { span: 16 },
          xs: { span: 16 },
          sm: { span: 16 }
        }
      }
    }
  },
  created() {
    const { detailId } = this.$props
    if (detailId) {
      this.formTile = '编辑'
      this.add = false
      this.id = detailId
      this.$nextTick(() => {
        this.getDetail()
      })
    } else {
      this.formTile = '新增'
      this.getUseTypeList()
      this.addField()
      this.add = true
    }
  },
  mounted() {
    this.getBrandList()
    // this.getRelStore()
  },
  methods: {
    remove(index) {
      let { fileds } = this
      fileds.splice(index, 1)
      this.fileds = fileds
      this.$nextTick(() => {
        this.realTimeNumberLimits = this.form.getFieldsValue().realTimeNumberLimits
      })
    },
    addField() {
      const obj = { type: undefined, num: '' }
      let { fileds } = this
      fileds.push(obj)
      this.fileds = fileds
    },
    changeStore(store) {
      this.store = store
      if (store == 0) {
        this.storeName = '全部门店'
      } else {
        this.storeName = '指定门店'
      }
    },
    changeNumtype() {
      this.$nextTick(() => {
        this.realTimeNumberLimits = this.form.getFieldsValue().realTimeNumberLimits
      })
    },
    changeNum() {
      this.$nextTick(() => {
        this.realTimeNumberLimits = this.form.getFieldsValue().realTimeNumberLimits
      })
    },
    changeMouth(mouth) {
      if (mouth == 0) {
        this.mouth = '成人生日'
      } else {
        this.mouth = '儿童生日'
      }
    },
    changeMember(value) {
      const member = []
      value.map(item => {
        this.cardList.map(subitem => {
          if (item == subitem.value) {
            member.push(subitem.name)
          }
        })
      })
      this.member = member.toString()
    },
    changeDay(value) {
      this.cardday = value
    },
    changePeople(value) {
      this.people = value
    },
    changePageTime(value) {
      this.buttonPageTime = value
    },
    changePageTime1(value) {
      this.buttonPageTime1 = value
    },
    changeJumpDays(value) {
      this.jumpDays = value
    },
    changeCardId(e) {
      this.cardId = e.target.value
    },
    changeqyConfig(e) {
      this.qyConfig = e.target.value
    },
    changePageUrl(e) {
      this.pageUrl = e.target.value
    },
    changePublic(value) {
      this.subTime = value
    },
    changePublic1(value) {
      this.subTime1 = value
    },
    changeNoUse(value) {
      this.noUseDay = value
    },
    changeTday(value) {
      this.tDay = value
    },
    changeType(type) {
      this.type = type
      if (type >= 5) {
        this.showPeople = false
      } else {
        this.showPeople = true
      }
      if (type == 8) {
        this.getCDepartList()
      }
      this.form.setFieldsValue({ peoplesType: '' })
      this.useTypeList.map(item => {
        if (item.id == type) {
          this.realTimePeoplesTypes = item.realTimePeoplesTypes
        }
      })
      // this.brand = ' '
      // this.storeName = ' '
      // this.mouth = ' '
      // this.cardday = ' '
      // this.member = ' '
      // this.tDay = ' '
      // this.cardId = ' '
      // this.noUseDay = ' '
    },
    changebrand(brand) {
      this.brand = brand
    },
    changeServe(value) {
      const newArr = []
      this.departList.map(item => {
        value.map(subitem => {
          if (item.id == subitem) {
            newArr.push(item.name)
          }
        })
      })
      this.serve = newArr.toString()
    },
    changeMode(value) {
      this.manageList.map(item => {
        if (item.value == value) {
          this.mode = item.name
        }
      })
    },
    reset() {
      this.form.resetFields()
      this.$emit('close')
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          
          if (values.buttonPageTime && values.buttonPageTime > values.buttonPageTime1) {
            this.$message.warning('请输入正确的进入页面时间范围！')
          } else if (values.subTime && values.subTime > values.subTime1) {
            this.$message.warning('请输入正确的关注公众号时间范围！')
          }else{
            this.submit(values)
          }
        }
      })
    },
    beforeUpload(file, fileList) {
      const { name } = file
      this.file = fileList
      this.file.slice(-1)
      const isExcel = /(xls|xlsx)$/i.test(name)
      if (!isExcel) {
        this.$message.error('请上传xlsx文件！')
        this.file = []
        return false
      } else {
        this.fileList = file
        this.importStore()
      }
    },

    removeFile() {
      this.file = []
      this.realImportStoreList = undefined
    },
    importStore() {
      const formData = new FormData()
      formData.append('file', this.fileList)
      try {
        axios({
          url: 'https://testbzhz.jnbygroup.com/api/gateway/basemgt-peoples/real/time/peoples/importStore',
          method: 'post',
          data: formData,
          contentType: false
        }).then(res => {
          if (res.code == 200) {
            this.realImportStoreList = res.data
          } else {
            this.$message.error(res.msg || res.message)
          }
        })
      } catch (error) {
        this.$message.error(error.msg || error.message)
      }
    },
    async getDetail() {
      const params = {
        id: this.id
      }
      try {
        const res = await getSetDetail('peoples', params)
        if (res.code == 200) {
          this.detail = res.data
          const detail = res.data
          this.appType = res.data.appType
          this.type = res.data.appType
          if (this.type > 4) {
            this.showPeople = false
          }
          if (this.type == 8) {
            this.getCDepartList()
          }
          
          this.people = detail.peoplesType
          this.$nextTick(() => {
            this.getUseTypeList()
            this.form.setFieldsValue({
              peoplesName: detail.peoplesName,
              appType: detail.appType,
              channel: detail.channel,
            })

            if (detail.consumptionTime) {
              this.form.setFieldsValue({
                consumptionTime: detail.consumptionTime
              })
              this.noUseDay = detail.consumptionTime
            }
            if (detail.jumpDays) {
              this.form.setFieldsValue({
                jumpDays: detail.jumpDays
              })
              this.jumpDays = detail.jumpDays
            }
            const isBandStore = detail.isBandStore + ''
            if (isBandStore) {
              this.form.setFieldsValue({
                isBandStore: Number(isBandStore)
              })
            }
            if (detail.type) {
              this.form.setFieldsValue({
                type: detail.type
              })
            }
            if (detail.bandBrand) {
              this.form.setFieldsValue({
                bandBrand: detail.bandBrand
              })
              this.brand = detail.bandBrand
            }
            if (detail.qyConfig) {
              this.form.setFieldsValue({
                qyConfig: detail.qyConfig
              })
              this.qyConfig = detail.qyConfig
            }
            if (detail.subTime) {
              this.form.setFieldsValue({
                subTime: detail.subTime,
                subTime1: detail.subTime1
              })
              this.subTime = detail.subTime
              this.subTime1 = detail.subTime1
            }
            if (detail.pageUrl) {
              this.form.setFieldsValue({
                pageUrl: detail.pageUrl
              })
              this.pageUrl = detail.pageUrl
            }
            if (detail.buttonPageTime) {
              this.form.setFieldsValue({
                buttonPageTime: detail.buttonPageTime,
                buttonPageTime1: detail.buttonPageTime1
              })
              this.buttonPageTime = detail.buttonPageTime
              this.buttonPageTime1 = detail.buttonPageTime1
            }
            if (detail.serverDis) {
              const arr = detail.serverDis.split(',').map(Number)
              this.form.setFieldsValue({
                serverDis: arr
              })
            }
            if (detail.storeMode != '-1') {
              this.form.setFieldsValue({
                storeMode: detail.storeMode
              })
              this.manageList.map(item => {
                if (item.value == detail.storeMode) {
                  this.mode = item.name
                }
              })
            }
            if (detail.realTimeNumberLimits.length > 0) {
              const arr = detail.realTimeNumberLimits.filter(item => {
                return delete item.id
              })
              const newArr = arr.filter(item => {
                return delete item.realId
              })
              var list = []
              newArr.map(item => {
                for (var key in item) {
                  if (item[key] > 0) {
                    const obj = {
                      type: key,
                      num: item[key]
                    }
                    list.push(obj)
                  }
                }
              })
              this.fileds = list
              this.realTimeNumberLimits = this.fileds
            }
            if (detail.memberLevel) {
              this.form.setFieldsValue({
                memberLevel: detail.memberLevel.split(',')
              })
              const member = []
              detail.memberLevel.split(',').map(item => {
                this.cardList.map(subitem => {
                  if (item == subitem.value) {
                    member.push(subitem.name)
                  }
                })
              })
              this.member = member.toString()
            }
            if (detail.cardDay) {
              this.form.setFieldsValue({
                cardDay: detail.cardDay
              })
              this.cardday = detail.cardDay
            }
            if (detail.bandTicket) {
              this.$nextTick(()=>{
                this.form.setFieldsValue({
                bandTicket: detail.bandTicket
              })
              })
              this.cardId = detail.bandTicket
            }
            if (detail.ticketDay) {
              this.form.setFieldsValue({
                ticketDay: Number(detail.ticketDay)
              })
              this.tDay = detail.ticketDay
            }
            if (detail.isBandStore == 1) {
              this.store = 1
              this.realImportStoreList = detail.realImportStoreList
            }
            if (detail.isBandStore == 0) {
              this.storeName = '全部门店'
            } else {
              this.storeName = '指定门店'
            }
            if (detail.type == 0) {
              this.mouth = '成人生日'
            } else {
              this.mouth = '儿童生日'
            }
          })
        } else {
          this.$message.warning(res.msg || res.message)
        }
        
      } catch (error) {
        console.log(error)
        // this.$message.warning(error.msg)
      }
    },
    async getUseTypeList() {
      const params = {}
      try {
        const res = await useTypeList('peoples', params)
        if (res.code == 200) {
          this.useTypeList = res.data
          if (this.appType) {
            this.useTypeList.map(item => {
              if (item.id == this.appType) {
                this.realTimePeoplesTypes = item.realTimePeoplesTypes
                this.$nextTick(() => {
                  this.form.setFieldsValue({ peoplesType: this.detail.peoplesType })
                })
                
              }
            })
          }
        } else {
          this.$message.warning(res.msg || res.message)
        }
      } catch (error) {
        this.$message.warning(error.msg)
      }
    },
    async getBrandList() {
      const params = {}
      try {
        const res = await brandList('peoples', params)
        if (res.code == 200) {
          this.brandList = res.data
        } else {
          this.$message.warning(res.msg || res.message)
        }
      } catch (error) {
        this.$message.warning(error.msg)
      }
    },
    async getRelStore() {
      const params = {}

      const res = await getRelStore('peoples', params)
      this.file = res
    },
    async getCDepartList() {
      const params = {}
      try {
        const res = await getCDepartList('peoples', params)
        if (res.code == 200) {
          this.departList = res.data
        } else {
          this.$message.warning(res.msg || res.message)
        }
      } catch (error) {
        this.$message.warning(error.msg)
      }
    },

    async submit(values) {
      this.loading = true
      var params = values
      // if (values.bandBrand) {
      //   // const bandBrand = values.bandBrand.toString()
      //   params.bandBrand = bandBrand
      // }
      if (values.memberLevel) {
        const memberLevel = values.memberLevel.toString()
        params.memberLevel = memberLevel
      }
      if (this.id) {
        params.id = this.id
      }
      if (values.isBandStore == 1) {
        params.realImportStoreList = this.realImportStoreList
      }
      if (values.serverDis) {
        params.serverDis = values.serverDis.toString()
      }
      if (values.realTimeNumberLimits) {
        const arr = []
        values.realTimeNumberLimits.map(item => {
          var obj = {}
          obj[item.type] = item.num
          arr.push(obj)
        })
        params.realTimeNumberLimits = arr
      }
      if (values.isBandStore == 1 && !this.realImportStoreList) {
        this.$message.warning('请导入指定门店！')
      } else {
        try {
          const res = this.add ? await insertPeoples('peoples', params) : await updatePeoples('peoples', params)
          if (res.code == 200) {
            this.$message.success(this.add ? '新建实时人群包成功！' : '修改实时人群包成功！')
            this.reset()
          } else {
            this.$message.warning(res.msg)
          }
        } catch (error) {
          this.$message.warning('操作失败！')
        } finally {
          this.loading = false
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.ml-20 {
  margin-left: 20px;
}
.submit {
  margin-left: 8px;
  background: #409eff;
  width: 110px;
  border-radius: 10px;
  height: 40px;
  color: #fff;
  text-align: center;
  line-height: 40px;
}
.reset {
  width: 110px;
  border-radius: 10px;
  height: 40px;
  border: 1px solid #969799;
  text-align: center;
  line-height: 40px;
}
.dynamic-delete-button {
  cursor: pointer;
  position: absolute;
  right: -26px;
  top: 8px;
  font-size: 24px;
  color: #999;
  transition: all 0.3s;
}
.dynamic-delete-button:hover {
  color: #777;
}
.dynamic-delete-button[disabled] {
  cursor: not-allowed;
  opacity: 0.5;
}
</style>
<style>
#export .ant-upload-list-item {
  position: absolute !important;
}
</style>